package monster.com.cvh.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

import com.baidu.mobstat.StatService;

import monster.com.cvh.R;
import monster.com.cvh.util.MyConstant;
import monster.com.cvh.util.SPUtils;

/**
 * Created by ASUS on 2017/11/27.
 */

public class SplashActivity extends AppCompatActivity {
    private static final String TAG = "SplashActivity";
    private final int SPLASH_DISPLAY_LENGHT = 2500;
    private Handler handler;
    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MyConstant.GO_GUIDE:
                    startActivity(new Intent(SplashActivity.this, GuideActivity.class));
                    finish();
                    break;
                case MyConstant.GO_Login:
                    if (SPUtils.getString(SplashActivity.this, "token", "").equals("")) {
                        startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                        finish();
                        Log.i(TAG, "登录: ");
                    } else {
                        startActivity(new Intent(SplashActivity.this, MainActivity.class));
                        Log.i(TAG, "自动登录: ");
                        finish();
                    }
                    break;
            }
        }
    };


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //全屏显示
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        //适配刘海
        if (Build.VERSION.SDK_INT >= 28) {
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
            getWindow().setAttributes(lp);
        }
        setContentView(R.layout.activity_splash);
        //百度统计埋点
        StatService.start(getApplication());
        jumpActivity();

    }

    private void jumpActivity() {
        boolean isFirstIn = SPUtils.getBoolean(SplashActivity.this, MyConstant.IS_FIRST_IN, true);
        if (isFirstIn) {
            mHandler.sendEmptyMessageDelayed(MyConstant.GO_GUIDE, 2 * 1000);
        } else {
            mHandler.sendEmptyMessageDelayed(MyConstant.GO_Login, 2 * 1000);
        }
    }
}
